package ks.client.gui.user.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ks.client.UserContext;
import ks.client.lobby.Lobby;
import ks.managers.icon.Icon;
import ks.managers.icon.IconManager;

public class ChangeIconClick implements ActionListener{
	
	Lobby lobby;
	UserContext context;
	IconManager im;
	
	public ChangeIconClick(Lobby lobby){
		this.lobby = lobby;
		context = lobby.getUserContext();
		im = context.getIconManager();
	}
	
	public void actionPerformed(ActionEvent e){
		// HACK TO SHOW YOU THAT THINGS ARE WORKING
		// Create temporary window in which these icons are to be shown.
		JFrame jf = new JFrame();
		jf.setTitle ("Icon List");
		jf.setSize(200, 400);
		jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		JPanel panel = new JPanel();
		panel.setLayout (new java.awt.FlowLayout());
		jf.add(panel);
		
		// add button for each icon.
		for (Icon icon : im.getList()) {
			ImageIcon imgIcon = new ImageIcon(icon.location);
			imgIcon.setDescription(icon.name);
			JButton button = new JButton(icon.name);
			button.setIcon(imgIcon);
			button.addActionListener(new IconClick(lobby, imgIcon));
			panel.add(button);
		}
		
		// make visible
		jf.setVisible(true);
		
		// HACK: show output
		lobby.append("There are " + im.getList().size() + " icons defined");
		
	}

}
